/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------


var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析

var virtualLink = require("mlib/util/virtualLink");
var jsonp = require("mlib/io/jsonp");
var when = require("mlib/util/when");
var wx = require("mlib/util/jweixin");
var touch = require("mlib/evt/touch");
var showMessage = require("../layer/info");
module.exports = function(node, opts) {
	
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;
	
    var rcoupon = null;
	
    //-------------事件响应声明---------------
    var evtFuncs = {
    	getDetail:function(evt){
        	//evt.cancelBubble = true;
    		if(!evt.target.hasAttribute("data-href")){
    			showMessage("奖品发放中,请耐心等待");
    		}
    	}
    }

    //-------------子模块实例化---------------
    var initMod = function() {
         virtualLink(function(evt){
			//_czc.push(["_trackEvent", "活动", "我的奖品", "点击领取奖品"]);
			console.log("evtTarget=>",evt.target.getAttribute("data-href"));
			if(!evt.target.getAttribute("data-href")){
				showMessage("奖品发放中,请耐心等待哦~");
			}else{
				location.href = evt.target.getAttribute("data-href");
			}
         	//location.href = evt.target.getAttribute("data-href");
         });
		custFuncs.getShareData().then(function(result) {
			// 微信分享	
			custFuncs.initWechat(result.data);
		});
    }

    //-------------绑定事件------------------
    var bindEvents = function() {
		touch.on(nodeList.detail,"tap",evtFuncs.getDetail);
    }

    //-------------自定义函数----------------
    var custFuncs = {
		initWechat: function (shareApiData) {

			var wxConfig = {
				debug: false,
				appId: shareApiData.appid,
				timestamp: parseInt(shareApiData.jstimestamp),
				nonceStr: shareApiData.jsnoncestr,
				signature: shareApiData.signature,
				jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'showMenuItems']
			};
			wx.config(wxConfig);

			wx.ready(function () {
				//要分享的数据
				wx.hideOptionMenu({
					"menuList": [
						'menuItem:openWithQQBrowser', //用QQ浏览器打开
						'menuItem:openWithSafari', //用Safari打开
						'menuItem:copyUrl', //复制链接
						'menuItem:share:qq', //分享到QQ
						"menuItem:share:appMessage",
						"menuItem:share:timeline",
						"menuItem:share:qq",
						"menuItem:share:weiboApp",
						"menuItem:favorite",
						"menuItem:share:facebook",
						"menuItem:share:QZone"
					]
				});
	
			});
		},
		getShareData: function() {
			var defer = when.defer();
			jsonp('http://m.happyvalleywh.smartoct.com/index.php?r=activity/share&url=' + encodeURIComponent(location.href), {}, function (err, result) {
				defer.resolve(result);
			});
			return defer.promise;
		}
    }

    //-------------一切从这开始--------------
    var init = function(_data) {
        data = _data;
        // 根据数据初始化模块
        // opts["render"]({ "title": data["title"] });

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);
        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();
    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};